home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13782 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.2 KB  |  43 lines

  1. Newsgroups: comp.lang.c
  2. Path: owl3.office-workstations-ltd.co.uk!not-for-mail
  3. From: kenn@office-workstations-ltd.co.uk (Ken Nicolson)
  4. Subject: Re: How to use assert( )
  5. Message-ID: <316b7c3a.2530799@newshost>
  6. Date: Wed, 10 Apr 1996 09:22:39 GMT
  7. References: <4kc3k7$dur@orion.cybercom.net> <smryanDpKxLz.39v@netcom.com>
  8. Organization: Office Workstations Limited
  9. Reply-To: kenn@owl-uk.co.uk
  10. X-Newsreader: Forte Agent .99d/32.182
  11.  
  12. smryan@netcom.com (@#$%!?!) wrote:
  13.  
  14. >    #include <assert.h>
  15. >
  16. >    assert(some_expression_which_must_be_true);
  17. >
  18. >as in 
  19. >
  20. >    assert(f=fopen("name","r"));
  21. >
  22. [snip!]
  23. >
  24. >    #define NDEBUG
  25. >    #include <assert.h>
  26. >
  27. >The assert(predicate) is elided.
  28. >
  29. That's true, but unfortunately the code in the assert body gets removed
  30. too, so your fopen call never gets compiled in the example above!
  31.  
  32. The simple rules to follow is never put useful code in the assert(), and
  33. never use it as a substitute for error checking, such as in the example
  34. above, as your code should gracefully handle a file not being opened, not
  35. chuck the user out with a fatal error message.
  36.  
  37. I only use assert() to check things I know *should* be true. Say during
  38. development if I accidentally call one of my own functions with a null
  39. pointer, I'm doing something wrong at the code level.
  40.  
  41. Ken
  42.  
  43.